<template>
  <el-dialog
    :title="!dataForm.resoldId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="二手房标题" prop="resoldTitle">
      <el-input v-model="dataForm.resoldTitle" placeholder="二手房标题"></el-input>
    </el-form-item>
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="dataForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="dataForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(县id)" prop="xianId">
      <el-input v-model="dataForm.xianId" placeholder="地区id(县id)"></el-input>
    </el-form-item>
    <el-form-item label="二手房地址" prop="resoldAddress">
      <el-input v-model="dataForm.resoldAddress" placeholder="二手房地址"></el-input>
    </el-form-item>
    <el-form-item label="二手房面积" prop="resoldArea">
      <el-input v-model="dataForm.resoldArea" placeholder="二手房面积"></el-input>
    </el-form-item>
    <el-form-item label="二手房朝向" prop="resoldDirection">
      <el-input v-model="dataForm.resoldDirection" placeholder="二手房朝向"></el-input>
    </el-form-item>
    <el-form-item label="二手房楼层" prop="resoldFloor">
      <el-input v-model="dataForm.resoldFloor" placeholder="二手房楼层"></el-input>
    </el-form-item>
    <el-form-item label="二手房价格" prop="resoldPrice">
      <el-input v-model="dataForm.resoldPrice" placeholder="二手房价格"></el-input>
    </el-form-item>
    <el-form-item label="二手房默认图片" prop="resoldImage">
      <el-input v-model="dataForm.resoldImage" placeholder="二手房默认图片"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="dimension">
      <el-input v-model="dataForm.dimension" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="装修情况（1 毛坯 2简装 3精装 4豪装）" prop="decolate">
      <el-input v-model="dataForm.decolate" placeholder="装修情况（1 毛坯 2简装 3精装 4豪装）"></el-input>
    </el-form-item>
    <el-form-item label="小区id" prop="cellId">
      <el-input v-model="dataForm.cellId" placeholder="小区id"></el-input>
    </el-form-item>
    <el-form-item label="小区名称" prop="cellName">
      <el-input v-model="dataForm.cellName" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="户型" prop="unitName">
      <el-input v-model="dataForm.unitName" placeholder="户型"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          resoldId: 0,
          resoldTitle: '',
          shenId: '',
          shiId: '',
          xianId: '',
          resoldAddress: '',
          resoldArea: '',
          resoldDirection: '',
          resoldFloor: '',
          resoldPrice: '',
          resoldImage: '',
          longitude: '',
          dimension: '',
          decolate: '',
          cellId: '',
          cellName: '',
          unitName: '',
          brokerId: ''
        },
        dataRule: {
          resoldTitle: [
            { required: true, message: '二手房标题不能为空', trigger: 'blur' }
          ],
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          xianId: [
            { required: true, message: '地区id(县id)不能为空', trigger: 'blur' }
          ],
          resoldAddress: [
            { required: true, message: '二手房地址不能为空', trigger: 'blur' }
          ],
          resoldArea: [
            { required: true, message: '二手房面积不能为空', trigger: 'blur' }
          ],
          resoldDirection: [
            { required: true, message: '二手房朝向不能为空', trigger: 'blur' }
          ],
          resoldFloor: [
            { required: true, message: '二手房楼层不能为空', trigger: 'blur' }
          ],
          resoldPrice: [
            { required: true, message: '二手房价格不能为空', trigger: 'blur' }
          ],
          resoldImage: [
            { required: true, message: '二手房默认图片不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          dimension: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          decolate: [
            { required: true, message: '装修情况（1 毛坯 2简装 3精装 4豪装）不能为空', trigger: 'blur' }
          ],
          cellId: [
            { required: true, message: '小区id不能为空', trigger: 'blur' }
          ],
          cellName: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          unitName: [
            { required: true, message: '户型不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.resoldId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.resoldId) {
            this.$http({
              url: this.$http.adornUrl(`/house/resold/info/${this.dataForm.resoldId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.resoldTitle = data.resold.resoldTitle
                this.dataForm.shenId = data.resold.shenId
                this.dataForm.shiId = data.resold.shiId
                this.dataForm.xianId = data.resold.xianId
                this.dataForm.resoldAddress = data.resold.resoldAddress
                this.dataForm.resoldArea = data.resold.resoldArea
                this.dataForm.resoldDirection = data.resold.resoldDirection
                this.dataForm.resoldFloor = data.resold.resoldFloor
                this.dataForm.resoldPrice = data.resold.resoldPrice
                this.dataForm.resoldImage = data.resold.resoldImage
                this.dataForm.longitude = data.resold.longitude
                this.dataForm.dimension = data.resold.dimension
                this.dataForm.decolate = data.resold.decolate
                this.dataForm.cellId = data.resold.cellId
                this.dataForm.cellName = data.resold.cellName
                this.dataForm.unitName = data.resold.unitName
                this.dataForm.brokerId = data.resold.brokerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/resold/${!this.dataForm.resoldId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'resoldId': this.dataForm.resoldId || undefined,
                'resoldTitle': this.dataForm.resoldTitle,
                'shenId': this.dataForm.shenId,
                'shiId': this.dataForm.shiId,
                'xianId': this.dataForm.xianId,
                'resoldAddress': this.dataForm.resoldAddress,
                'resoldArea': this.dataForm.resoldArea,
                'resoldDirection': this.dataForm.resoldDirection,
                'resoldFloor': this.dataForm.resoldFloor,
                'resoldPrice': this.dataForm.resoldPrice,
                'resoldImage': this.dataForm.resoldImage,
                'longitude': this.dataForm.longitude,
                'dimension': this.dataForm.dimension,
                'decolate': this.dataForm.decolate,
                'cellId': this.dataForm.cellId,
                'cellName': this.dataForm.cellName,
                'unitName': this.dataForm.unitName,
                'brokerId': this.dataForm.brokerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
